﻿
Imports System.Web.HttpContext

Public Class FMWBO
    Shared songDataFile As String
    Shared errorDataFile As String

    Shared lstSongs As List(Of Song)

    Shared Sub New()
        'Load all the objects in memory for faster performance
        'songDataFile = "F:\MyRobots\MyRobots\FMW.Win\Data\songs.xml"
        songDataFile = System.Web.HttpContext.Current.Server.MapPath("~/data/fmw/songs.xml")
        errorDataFile = System.Web.HttpContext.Current.Server.MapPath("~/data/fmw/errors.xml")

        'Dim dr = Current.Server.MapPath("~/data/fmw")

        'If Not System.IO.Directory.Exists(dr) Then
        '    System.IO.Directory.CreateDirectory(dr)
        '    System.IO.File.Create(dr & "/12.txt")
        '    Throw New Exception("fads")
        'End If

        'If Not System.IO.File.Exists(songDataFile) Then
        '    Throw New Exception("server exception")
        'End If

        'If Not System.IO.File.Exists(songDataFile) Then

        'End If

        lstSongs = New List(Of Song)

        ReadAllSongs()

        Dim lastFetchID = (From t In lstSongs Order By t.ID Descending Select t.ID).FirstOrDefault

        If lastFetchID = 0 Then
            lastFetchID = 1 'aas its givign error so we make a rule there.
        End If

        Dim th As New Threading.Thread(AddressOf DoProcess)
#If Not Debug Then
        th.Start(lastFetchID + 1)
#End If


    End Sub

    Private Shared Sub DoProcess(ByVal initID As Integer)
        '''''http://www.apunkabollywood.net/browser/download/get/1808'''
        Dim i As Integer = initID
        Dim wc As New System.Net.WebClient
        Do
            Try
                If Not songsIDCollection.Contains(i) Then
                    Dim songURL = String.Format("http://208.78.41.223/browser/download/get/{0}", i)
                    Dim data = wc.DownloadString(songURL)
                    Dim o = Song.Transform(data)
                    If o IsNot Nothing Then
                        o.ID = i
                        o.PageURL = songURL
                        SyncLock lstSongs
                            SaveSong(o)
                            lstSongs.Add(o)
                        End SyncLock
                    Else
                        Throw New Exception("data not found for songurl : " & songURL)
                    End If
                End If
            Catch ex As Exception
                WriteError(ex.Message)
                If i > 70000 Then
                    System.Threading.Thread.Sleep(60000)
                End If
            Finally
                i += 1
            End Try
        Loop
    End Sub

    Private Shared Sub SaveSong(ByVal o As Song)

        Dim x = <song>
                    <id><%= o.ID %></id>
                    <title><%= o.Title %></title>
                    <downloadurl><%= o.DownloadURL %></downloadurl>
                    <dirpath><%= o.DirPath %></dirpath>
                    <pageurl><%= o.PageURL %></pageurl>
                    <movietitle><%= o.MovieTitle %></movietitle>
                </song>

        'Dim cnt As Integer

        'SyncLock lstSongs
        '    cnt = (From t In lstSongs Where t.ID = o.ID).Count
        'End SyncLock

        If Not songsIDCollection.Contains(o.ID) Then
            System.IO.File.AppendAllText(songDataFile, x.ToString(SaveOptions.DisableFormatting))
        Else
            WriteError("Song with ID " & o.ID & "already exisist")
        End If

    End Sub

    Shared ReadOnly Property songsIDCollection() As List(Of Integer)
        Get
            SyncLock lstSongs
                Dim c = (From t In lstSongs Select t.ID).ToList
                Return c
            End SyncLock
        End Get
    End Property

    Private Shared Sub WriteError(ByVal m As String)
        Dim errorToWrite = <error>
                               <ex><%= m %></ex>
                               <timer><%= Now.ToString("dd-MM-yyyy hh:mm:ss") %></timer>
                           </error>.ToString(SaveOptions.DisableFormatting)
        System.IO.File.AppendAllText(errorDataFile, errorToWrite)
    End Sub

    Private Shared Sub ReadAllSongs()
        Dim songsData = System.IO.File.ReadAllText(songDataFile)
        Dim songsXml = System.Xml.Linq.XElement.Parse("<x>" & songsData & "</x>")

        Dim objList = From t In songsXml...<song>
                      Select New Song With {.Title = t...<title>.Value,
                                            .DownloadURL = t...<downloadurl>.Value,
                                            .DirPath = t...<dirpath>.Value,
                                            .PageURL = t...<pageurl>.Value,
                                            .MovieTitle = t...<movietitle>.Value,
                                            .ID = t...<id>.Value
                                           }



        'Dim q = (From t In songsXml...<song> Select "INSERT INTO [db1].[dbo].[tb2]           ([title]           ,[download_url]           ,[dir_path]           ,[page_url] ,[movie_title]           ,[id]) " & _
        '        String.Format("values('{0}','{1}','{2}','{3}','{4}','{5}') ", t...<title>.Value.Replace("'", "''"), t...<downloadurl>.Value.Replace("'", "''"), t...<dirpath>.Value.Replace("'", "''"), t...<pageurl>.Value.Replace("'", "''"), t...<movietitle>.Value.Replace("'", "''"), t...<id>.Value.Replace("'", "''"))).ToArray

        'Dim query = String.Join(vbCrLf, q)
        'System.IO.File.WriteAllText("C:\12.txt", query)
        SyncLock lstSongs
            lstSongs.AddRange(objList)
        End SyncLock
    End Sub

    Public Shared Function ListSongs(ByVal q As String, ByVal pageIndex As Integer, ByVal pageSize As Integer, ByRef count As Integer, ByVal filter As SongFilterType) As List(Of Song)
        SyncLock lstSongs
            Dim lstQueryFilter = (From t In lstSongs Where (t.Title.ToLower.Contains(q.ToLower) AndAlso filter = SongFilterType.SongTitle) OrElse
                     (t.DirPath.ToLower.Contains(q.ToLower) AndAlso filter = SongFilterType.AlbumTitle) OrElse
                     ((t.Title.ToLower.Contains(q.ToLower) OrElse t.DirPath.ToLower.Contains(q.ToLower)) AndAlso filter = SongFilterType.All)
                     Select t)

            count = lstQueryFilter.Count

            Dim o = (From t In lstQueryFilter Skip pageIndex * pageSize Take pageSize Select t).ToList
            Return o
        End SyncLock
    End Function

    Public Shared Function GetTop5LastUpdatedSongs() As List(Of Song)
        SyncLock lstSongs
            Dim o = (From t In lstSongs Order By t.ID Descending Take 5 Select t).ToList
            Return o
        End SyncLock

    End Function

    Public Shared ReadOnly Property SongsCount As Integer
        Get
            SyncLock lstSongs
                Return lstSongs.Count
            End SyncLock

        End Get
    End Property

    Public Shared Function GetSongByID(ByVal id As Integer) As Song
        Dim s = (From t In lstSongs Where t.ID = id Select t).FirstOrDefault
        Return s
    End Function

    Enum SongFilterType
        All = 0
        SongTitle = 1
        AlbumTitle = 2
    End Enum
End Class

Public Class Movie
    Property Title As String
    Property PageURL As String
    Property Year As String
End Class

Public Class Song

    Property ID As Integer

    Property Title As String
    Property PageURL As String
    Property DownloadURL As String
    Property MovieTitle As String
    Property DirPath As String

    Shared Function Transform(ByVal s As String) As Song
        Dim objSong As New Song()

        s = System.Text.RegularExpressions.Regex.Replace(s, "\t|\n|\r", "")
        Dim rxDwldURL = System.Text.RegularExpressions.Regex.Match(s, "http://download[^""]*")

        If rxDwldURL.Success Then
            objSong.DownloadURL = rxDwldURL.Value
            objSong.ParseTitle()
        Else
            Return Nothing
        End If

        Dim rxDirPath = System.Text.RegularExpressions.Regex.Match(s, "<div id=""dvdirpath"">.*?</div>")

        If rxDirPath.Success Then
            Dim dirPathComplete = rxDirPath.Value
            Dim rxDirLinks = System.Text.RegularExpressions.Regex.Matches(dirPathComplete, "<a[^>]*?>.*?</a>")
            For Each rxMatch As System.Text.RegularExpressions.Match In rxDirLinks
                Dim dirPathValue = rxMatch.Value
                dirPathValue = System.Text.RegularExpressions.Regex.Replace(dirPathValue, "<[^>]*>", "")

                If String.IsNullOrEmpty(objSong.DirPath) Then
                    objSong.DirPath = dirPathValue
                Else
                    objSong.DirPath &= " / " & dirPathValue
                End If
            Next
        End If

        Return objSong

    End Function

    Private Sub ParseTitle()
        Dim objSplitItems = Me.DownloadURL.Split("/").ToList
        Me.Title = objSplitItems.Item(objSplitItems.Count - 1)

        


    End Sub

    ReadOnly Property DisplayTitle As String
        Get
            Dim rx As String = ""
            Dim lst As New List(Of String)

            lst.Add(" @ fmw11.com")
            lst.Add(" \(fmw11.com\)")
            lst.Add("-www.fmw11.com-")
            lst.Add("\(www.fmw11.com\)")
            lst.Add("-www.fmw11com-")
            lst.Add("{fmw11.com}")
            lst.Add(" \(papuyaar.com\)")
            lst.Add("\(papuyaar - com\)")
            lst.Add("\(papuiyaar.com\)")
            lst.Add("\(papuyar.com\)")
            lst.Add("@ papuyar.com\)")
            lst.Add("\(papuyar com\)")
            lst.Add("\[www.fmw11.com\]")
            lst.Add("@ fmw")
            lst.Add("\(fmw11\)")
            lst.Add("\(papuyaar com\)")
            lst.Add("(.mp3)")


            Dim ss = (From t In lst Select "(" & t & ")").ToArray
            rx = String.Join("|", ss)

            Dim x = System.Text.RegularExpressions.Regex.Replace(Me.Title, rx, "", Text.RegularExpressions.RegexOptions.IgnoreCase)

            Return x.Trim()
        End Get
    End Property

    'Public Shared Function CleanUpTitle(ByVal s As String) As String
    '    Return s.Replace("@ fmw", "").
    '                Replace("(fmw11.com)", "").
    '                Replace("-www.fmw11.com-", "").
    '                Replace("(www.fmw11.com)", "").
    '                Replace("-www.fmw11com-", "").
    '                Replace("{fmw11.com}", "").
    '                Replace("(fmw11)", "").
    '                Replace("(papuyaar.com)", "").
    '                Replace("(papuyaar - com)", "").
    '                Replace("(papuiyaar.com)", "").
    '                Replace("(papuyar.com)", "").
    '                Replace("@ papuyar.com)", "").
    '                Replace("(papuyar com)", "").
    '                Replace("[www.fmw11.com]", "").
    '                Replace("(papuyaar com)", "")



    '    '        SELECT * FROM tb2 WHERE 
    '    '--tb2.title LIKE '%fmw%'and 
    '    'id NOT IN (
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%@ fmw%'
    '    '--UNION
    '    '--SELECT id FROM tb2 WHERE tb2.title LIKE '%papuyaar%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%(fmw11.com)%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%fmw11.com%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%-www.fmw11.com-%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%(www.fmw11.com)%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%-www.fmw11com-%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%{fmw11.com}%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%(fmw11)%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%(papuyaar.com)%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%(papuyaar - com)%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%(papuiyaar.com)%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%(papuyar.com)%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%@ papuyar.com)%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%(papuyar com)%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%(papuyaar com)%'
    '    '--UNION
    '    '--SELECT id FROM tb2 WHERE tb2.title LIKE '%[www.fmw11.com]%'
    '    ')



    '    'SELECT * FROM tb2 WHERE 
    '    'tb2.title LIKE '%fmw%' and 
    '    'id NOT IN (
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%@ fmw%'
    '    '--UNION
    '    '--SELECT id FROM tb2 WHERE tb2.title LIKE '%papuyaar%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%(fmw11.com)%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%{fmw11.com }%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '% - [www.fmw11.com ]%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%- [www.fmw11.com]%'
    '    'UNION
    '    'SELECT * FROM tb2 WHERE tb2.title LIKE '% - [www.fmw11.com]%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%-www.fmw11.com-%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%(www.fmw11.com)%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%-www.fmw11com-%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%{fmw11.com}%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%(fmw11)%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%(papuyaar.com)%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%(papuyaar - com)%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%(papuiyaar.com)%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%(papuyar.com)%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%@ papuyar.com)%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%(papuyar com)%'
    '    'UNION
    '    'SELECT id FROM tb2 WHERE tb2.title LIKE '%(papuyaar com)%'
    '    '--UNION
    '    '--SELECT id FROM tb2 WHERE tb2.title LIKE '%[www.fmw11.com]%'
    '    ')

    'End Function
End Class
